package com.lanou.fooddemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.bean.SubCardsBean;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dlios on 15-8-7.
 */
//话题详情页适配器
public class TopicDetailsAdapter extends BaseAdapter {

    private static final String LOG_TAG = "TopicDetailsAdapter"; //打印

    private LayoutInflater inflater;
    private List<DataBean> dataBeans;
    private List<SubCardsBean> subCardsBeans;

    public TopicDetailsAdapter(Context c, List<DataBean> beans) {  //构造方法传参
        inflater = LayoutInflater.from(c);
        dataBeans = beans;
    }


    @Override
    public int getCount() {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.size() - 4;
        }
        return 0;
    }


    @Override
    public Object getItem(int position) {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.get(position);
        }
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        subCardsBeans = new ArrayList<SubCardsBean>();
        Holder holder = null;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.topic_details_item, null);
            holder = new Holder();
            holder.topicDetailsItemBoxTheme =
                    (TextView) convertView.findViewById(R.id.topicDetailsItemBoxTheme);
            holder.topicDetailsItemBox =
                    (SmartImageView) convertView.findViewById(R.id.topicDetailsItemBox);
            holder.topicDetailsItemUserName =
                    (TextView) convertView.findViewById(R.id.topicDetailsItemUserName);
            holder.topicDetailsItemUserSign =
                    (TextView) convertView.findViewById(R.id.topicDetailsItemUserSign);
            holder.topicDetailsItemUserIcImageView =
                    (SmartImageView) convertView.findViewById(R.id.topicDetailsItemUserIcImageView);
            holder.topicDetailsItemFoodImageView =
                    (SmartImageView) convertView.findViewById(R.id.topicDetailsItemFoodImageView);
            holder.topicDetailsItemDescribe =
                    (TextView) convertView.findViewById(R.id.topicDetailsItemDescribe);
            holder.topicDetailsItemThumbUp =
                    (TextView) convertView.findViewById(R.id.topicDetailsItemThumbUp);

            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        DataBean dataBean = null;
            if (CommonUtil.isListNotNull(dataBeans)) {
                dataBean = dataBeans.get(position +4);
                if (null != dataBean.getTitle()) {
                    Log.i("liuyan", "subCardsBean title= " + dataBean.getTitle());
                }
                if (null != dataBean.getSubCards() && DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {
                    subCardsBeans = dataBean.getSubCards();
                }
            }
            if (CommonUtil.isListNotNull(subCardsBeans)) {
                Log.i("liuyan", "subCardsBeans != null enter");
                SubCardsBean subCardsBean = null;
                SubCardsBean subCardsBeanSeparator = null;
                SubCardsBean subCardsBeanImage = null;
                SubCardsBean subCardsBeanContent = null;
                Log.i("liuyan", "subCardsBeanList size = " + subCardsBeans.size());
                for (int i = 0; i < subCardsBeans.size(); i++) {
                    SubCardsBean subCardsBeanTemp = subCardsBeans.get(i);
                    Log.i("liuyan", "temp title = " + subCardsBeanTemp.getTitle());
                    if (null != subCardsBeanTemp.getTitle()) {
                        Log.i("liuyan", "subCardsBean title= " + subCardsBeanTemp.getTitle());
                    }
                    if (SubCardsBean.TYPE_SEPARATOR_CARD.equals(subCardsBeanTemp.getType()) && i == 0) {
                        Log.i("liuyan", "hua ti title = " + subCardsBeanTemp.getTitle() + subCardsBeanTemp.getId());
                        subCardsBeanSeparator = subCardsBeanTemp;
                    }

                    if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBeanTemp.getType())) {
                        subCardsBean = subCardsBeanTemp;
                    }
                    if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType()) ||
                            SubCardsBean.TYPE_SLIDE_CARD.equals(subCardsBeanTemp.getType())) {
                        subCardsBeanImage = subCardsBeanTemp;
                    }

                    if (SubCardsBean.TYPE_CONTENT_CARD.equals(subCardsBeanTemp.getType())) {
                        subCardsBeanContent = subCardsBeanTemp;
                    }
                }
                holder.topicDetailsItemBox.setImageUrl(subCardsBeanSeparator.getIcon().getUrl());
                holder.topicDetailsItemBoxTheme.setText(subCardsBeanSeparator.getTitle());
                holder.topicDetailsItemUserIcImageView.setImageUrl(subCardsBean.getUser().getAvatar(),
                        SmartImageView.TYPE_ROUND);
                holder.topicDetailsItemUserName.setText(subCardsBean.getTitle());
                holder.topicDetailsItemUserSign.setText(subCardsBean.getDesc());
                if (subCardsBeanContent != null) {
                    holder.topicDetailsItemDescribe.setText(subCardsBeanContent.getContent());
                }


                String imageUrl = null;
                if (null == subCardsBeanImage.getPics()) {
                    imageUrl = subCardsBeanImage.getPic().getUrl();
                } else {
                    imageUrl = subCardsBeanImage.getPics().get(0).getUrl();
                }
                Log.i(LOG_TAG, "image = " + imageUrl);
                holder.topicDetailsItemFoodImageView.setImageUrl(imageUrl);
            }
        return convertView;
//
//    //    if (0 == position) {
//      //      convertView = inflater.inflate(R.layout.topic_details_header, null);
//
////            DataBean dataBean = null;
////            DataBean userCardBean = dataBeans.get(1);
////            DataBean ContentCardBean = dataBeans.get(2);
////            if (CommonUtil.isListNotNull(dataBeans)) {
////                dataBean = dataBeans.get(0);
////
////            }
////
////            //解析EventCard
////            SmartImageView topicDetails_header_picture = (SmartImageView) convertView.findViewById(R.id.topicDetails_header_picture);
////            TextView topicDetails_header_Theme = (TextView) convertView.findViewById(R.id.topicDetails_header_Theme);
////            TextView topicDetails_header_ThemeNumber = (TextView) convertView.findViewById(R.id.topicDetails_header_ThemeNumber);
////            topicDetails_header_picture.setImageUrl(dataBean.getPic().getUrl());
////            topicDetails_header_Theme.setText(dataBean.getTitle());
////            topicDetails_header_ThemeNumber.setText(dataBean.getDesc());
////
////            //解析UserCard
////            SmartImageView topicDetails_header_headPortrait = (SmartImageView) convertView.findViewById(R.id.topicDetails_header_headPortrait);
////            TextView topicDetails_header_UserName = (TextView) convertView.findViewById(R.id.topicDetails_header_UserName);
////            TextView topicDetails_header_UserSign = (TextView) convertView.findViewById(R.id.topicDetails_header_UserSign);
////            topicDetails_header_UserName.setText(userCardBean.getTitle());
////            topicDetails_header_UserSign.setText(userCardBean.getDesc());
////            topicDetails_header_headPortrait.setImageUrl(userCardBean.getUser().getAvatar());
////
////            //解析contentcard
////            TextView topicDetails_header_describe = (TextView) convertView.findViewById(R.id.topicDetails_header_describe);
////            topicDetails_header_describe.setText(ContentCardBean.getContent());
//
//         if (0 <= position && position <= dataBeans.size() - 3) {
//            position += 3;
//            Holder holder = null;
//            convertView = inflater.inflate(R.layout.topic_details_item, null);
//            holder = new Holder();
//            holder.topicDetailsItemBoxTheme =
//                    (TextView) convertView.findViewById(R.id.topicDetailsItemBoxTheme);
//            holder.topicDetailsItemBox =
//                    (SmartImageView) convertView.findViewById(R.id.topicDetailsItemBox);
//            holder.topicDetailsItemUserName =
//                    (TextView) convertView.findViewById(R.id.topicDetailsItemUserName);
//            holder.topicDetailsItemUserSign =
//                    (TextView) convertView.findViewById(R.id.topicDetailsItemUserSign);
//            holder.topicDetailsItemUserIcImageView =
//                    (SmartImageView) convertView.findViewById(R.id.topicDetailsItemUserIcImageView);
//            holder.topicDetailsItemFoodImageView =
//                    (SmartImageView) convertView.findViewById(R.id.topicDetailsItemFoodImageView);
//            holder.topicDetailsItemDescribe =
//                    (TextView) convertView.findViewById(R.id.topicDetailsItemDescribe);
//            holder.topicDetailsItemThumbUp =
//                    (TextView) convertView.findViewById(R.id.topicDetailsItemThumbUp);
//
//            DataBean dataBean = null;
//            if (CommonUtil.isListNotNull(dataBeans)) {
//                dataBean = dataBeans.get(position);
//                if (null != dataBean.getTitle()) {
//                    Log.i("liuyan", "subCardsBean title= " + dataBean.getTitle());
//                }
//                if (null != dataBean.getSubCards() && DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {
//                    subCardsBeans = dataBean.getSubCards();
//                }
//            }
//            if (CommonUtil.isListNotNull(subCardsBeans)) {
//                Log.i("liuyan", "subCardsBeans != null enter");
//                SubCardsBean subCardsBean = null;
//                SubCardsBean subCardsBeanSeparator = null;
//                SubCardsBean subCardsBeanImage = null;
//                SubCardsBean subCardsBeanContent = null;
//                Log.i("liuyan", "subCardsBeanList size = " + subCardsBeans.size());
//                for (int i = 0; i < subCardsBeans.size(); i++) {
//                    SubCardsBean subCardsBeanTemp = subCardsBeans.get(i);
//                    Log.i("liuyan", "temp title = " + subCardsBeanTemp.getTitle());
//                    if (null != subCardsBeanTemp.getTitle()) {
//                        Log.i("liuyan", "subCardsBean title= " + subCardsBeanTemp.getTitle());
//                    }
//                    if (SubCardsBean.TYPE_SEPARATOR_CARD.equals(subCardsBeanTemp.getType()) && i == 0) {
//                        Log.i("liuyan", "hua ti title = " + subCardsBeanTemp.getTitle() + subCardsBeanTemp.getId());
//                        subCardsBeanSeparator = subCardsBeanTemp;
//                    }
//
//                    if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBeanTemp.getType())) {
//                        subCardsBean = subCardsBeanTemp;
//                    }
//                    if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType()) ||
//                            SubCardsBean.TYPE_SLIDE_CARD.equals(subCardsBeanTemp.getType())) {
//                        subCardsBeanImage = subCardsBeanTemp;
//                    }
//
//                    if (SubCardsBean.TYPE_CONTENT_CARD.equals(subCardsBeanTemp.getType())) {
//                        subCardsBeanContent = subCardsBeanTemp;
//                    }
//                }
//                holder.topicDetailsItemBox.setImageUrl(subCardsBeanSeparator.getIcon().getUrl());
//                holder.topicDetailsItemBoxTheme.setText(subCardsBeanSeparator.getTitle());
//                holder.topicDetailsItemUserIcImageView.setImageUrl(subCardsBean.getUser().getAvatar(),
//                        SmartImageView.TYPE_ROUND);
//                holder.topicDetailsItemUserName.setText(subCardsBean.getTitle());
//                holder.topicDetailsItemUserSign.setText(subCardsBean.getDesc());
//                if (subCardsBeanContent != null) {
//                    holder.topicDetailsItemDescribe.setText(subCardsBeanContent.getContent());
//                }
//
//
//                String imageUrl = null;
//                if (null == subCardsBeanImage.getPics()) {
//                    imageUrl = subCardsBeanImage.getPic().getUrl();
//                } else {
//                    imageUrl = subCardsBeanImage.getPics().get(0).getUrl();
//                }
//                Log.i(LOG_TAG, "image = " + imageUrl);
//                holder.topicDetailsItemFoodImageView.setImageUrl(imageUrl);
////
////                    holder.topicDetailsItemDescribe.setText(subCardsBeanContent.getContent());
////                    holder.topicDetailsItemThumbUp.setText(subCardsBeanMap.getTitle());
//
//
//            }
//
//        }
//
//
//        return convertView;
//



    }
    class Holder {
        SmartImageView topicDetailsItemUserIcImageView;
        TextView topicDetailsItemUserName;
        TextView topicDetailsItemUserSign;
        SmartImageView topicDetailsItemFoodImageView;
        TextView topicDetailsItemDescribe;
        TextView topicDetailsItemThumbUp;
        TextView topicDetailsItemBoxTheme;
        SmartImageView topicDetailsItemBox;
    }

}